/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.server.db;

import java.io.InputStream;
import java.sql.ResultSet;
import java.util.Date;
import java.util.List;

import de.uhilger.netzpult.shared.Document;
import de.uhilger.netzpult.shared.Folder;
import de.uhilger.netzpult.shared.GroupShare;
import de.uhilger.netzpult.shared.UserGroup;
import de.uhilger.netzpult.shared.UserShare;

/**
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public interface DataStorage {
	
	public static final String NAME = "datastorage";
	
	public static final String KEY_DOC_ID = "dc_id";
	public static final String KEY_FOLDER_ID = "fo_id";

	public static final boolean WITHOUT_DRAFT = true;
	public static final boolean WITH_DRAFT = false;
	
	public static final boolean WITH_CONTENT = true;
	public static final boolean WITHOUT_CONTENT = false;
	
	/* ---------------- Document Management ----------------- */

	public Document createDocument(Document document, String owner) throws Exception;
	public Document createDocument(Document document, String owner, InputStream content, int len) throws Exception;
	public void createNewVersion(Document document, String owner, InputStream content, int len) throws Exception;
	public Document createNewVersion(Document document) throws Exception;
	public Document saveDraft(Document document) throws Exception;
	public int deleteDocument(int docId) throws Exception;
	
	public List<Document> findDocuments(String owner, String searchItem) throws Exception;
	public String reindex() throws Exception;
	
	public boolean isPermitted(String userId, int documentId, int requestedPermission) throws Exception;
	
	public List<UserShare> getUserShares(int docId);
	public UserShare addUserShare(UserShare share);
	public UserShare updateUserShare(UserShare share);
	public UserShare deleteUserShare(UserShare share);
	
	public List<GroupShare> getGroupShares(int docId);
	public GroupShare addGroupShare(GroupShare share);
	public GroupShare updateGroupShare(GroupShare share);
	public GroupShare deleteGroupShare(GroupShare share);
	
	public List<UserGroup> getUserGroups(String owner);
	public UserGroup addUserGroup(UserGroup group);
	public UserGroup updateUserGroups(UserGroup group);
	public UserGroup deleteUserGroup(UserGroup group);
	
	public void close(ResultSet rs) throws Exception;
	public ResultSet getDocumentAsResultSet(int id, boolean ignoreDraft) throws Exception;
	public Document getDocument(int id, boolean ignoreDraft, boolean withContent) throws Exception;
	public Document getDocument(int id, int version, boolean withContent) throws Exception;
	public List<Document> getUnfiledDocuments(String owner, boolean withoutDraft) throws Exception;
	public List<Document> getDraftDocuments(String owner) throws Exception;
	public List<Document> getSharedDocuments(String owner) throws Exception;
	public int getLastVersion(int documentId, boolean ignoreDraft) throws Exception;
	public String getDocumentOwner(int documentId) throws Exception;
	public boolean isPublic(int documentId) throws Exception;
	
	public String getCreationTimeStrLong(String creationTime);
	public Date getDocumentDate(String creationTime);
	
	/* ---------------- Folder Management ------------------- */ 
	
	public Folder createFolder(Folder folder, String owner) throws Exception;
	public Folder updateFolder(Folder folder) throws Exception;
	public void deleteFolder(Folder folder) throws Exception;
	public Folder moveFolder(Folder folder, int newParentId);
	public String getFolderOwner(Folder folder);
	
	public List<Folder> getChildren(Folder folder) throws Exception;
	public List<Folder> getChildren(Folder folder, String owner) throws Exception;
	public List<Folder> getRootFolders(String owner) throws Exception;
	public List<Folder> getPublicRootFolders(String owner) throws Exception;
	
	public void addUserToGroup(int groupId, String userId);
	public void removeUserFromGroup(int groupId, String userId);
	
	public void addToFolder(Folder folder, int documentId) throws Exception;
	public void removeFromFolder(Folder folder, int documentId) throws Exception;	

	public List<Document> getDocuments(String owner, Folder folder, boolean withoutDraft) throws Exception;
	public List<Document> getDocuments(String owner, Folder folder, boolean withoutDraft, String orderClause, 
			int offset, int count) throws Exception;
	
	public int getLastFolderOrderNo(int parentFolderId) throws Exception;
	
	public int upFolder(int folderId) throws Exception;
	public int downFolder(int folderId) throws Exception;
	
	public int upDocument(int folderId, int documentId) throws Exception;
	public int downDocument(int folderId, int documentId) throws Exception;
	
	public List<Folder> getFoldersContaining(int documentId) throws Exception;
	public List<Folder> getFolderPath(Folder folder) throws Exception;
	public List<Folder> getFolder(int parentId, String name) throws Exception;
	
	/* ------------------ Export -------------------- */
	
	public ResultSet getAllDocuments() throws Exception; 	
	public String getCreationTimeStrLong(Date date);
	
}
